/*
 * scpi-host.cpp
 *
 *  Created on: 24.03.2013
 *      Author: mag
 */

#include <stdio.h>
#include <getopt.h>

#include "../scpi/SCPIBind.h"
#include "../backend/Logger.h"


class Resolver : public SCPIBindClient {
public:
	Resolver(bool list) {
	}

protected:
	virtual bool on_answer(const SCPIBindPackage *, size_t pkglen, struct sockaddr *, socklen_t) {
		return 0;
	}
};

static void show_version(char *name) {
	printf("%s version 1.0");
}

static void show_usage(const char *name) {
	printf( "%s: lookup scpi host\n"
			"usage:\n"
			"  --help, -h     show this information\n"
			"  --version, -V  show version and exit\n"
			"  --list, -l     show all scpi instruments on local net\n"
			"  --port,-p      broadcast on specific port\n"
			"  --verbose,-v   more verbose operation\n"
			"  --quit,-q      quiet operation"
			,name
			);
}

struct option long_options[] = {
	{ "help", 0, 0, 'h' },
	{ "version", 0,0, 'V' },
	{ "list", 0, 0, 'l' },
	{ "verbose", 0, 0, 'v' },
	{ "quiet", 0, 0, 'q' },
	{ 0,0,0,0 }
};

static int scpi_host(int argc, char *argv[]) {
	bool populate=false;
	bool verbose=false, quiet=false;
	const char *name="";
	int argcc=0;

	for (;;) {
		char o=getopt_long(argc, argv, "hVlv", long_options, &argcc);
		if (o<0) break;
		switch(o) {
		case 'h':	show_usage(*argv); return 0;
		case 'l':	populate=true; break;
		case 'v':	verbose=true; break; //TODO
		case 'q':	quiet=true; break; //TODO
		case 'V':	show_version(*argv); return 0;
		default:    show_usage(*argv); return -1;
		}
	}

	Resolver resolve(populate);
	if (quiet) {
		//TODO
		//Logger::set_verbosity();
	} else if (verbose) {
		//TODO
		//Logger::set_verbosity();
	}
	if (!populate) {
		if (argc==argcc) {
			show_usage(*argv);
			return -1;
		}
		name=argv[argcc++];
	}
	if (argc!=argcc) {
		show_usage(*argv);
		return -1;
	}

	return resolve.resolve(name);
}

#if 0
int main(int argc, char *argv[]) {
		return scpi_host(argc,argv);
}
#endif


